define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shoproom/index' + location.search,
                    add_url: 'shop/shoproom/add',
                    edit_url: 'shop/shoproom/edit',
                    del_url: 'shop/shoproom/del',
                    multi_url: 'shop/shoproom/multi',
                    import_url: 'shop/shoproom/import',
                    table: 'shop_room',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('名字'), operate: 'LIKE'},
                        {field: 'tag', title: __('标签'), operate: false, formatter: Table.api.formatter.flag},
                        {field: 'logo', title: __('Logo'),  events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false},
                        {field: 'shop_id', title: __('店铺ID')},
                        {field: 'shop.name', title: __('店铺名字')},
                        {field: 'device_sn', title: __('绑定主板'), operate: false},

                        {field: 'min_book_hour', title: __('最低起订时间'), operate:false},
                        {field: 'hour_price', title: __('价格'), operate:false},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,

                            buttons: [

                                {
                                    name: 'list',
                                    title: __('包厢套餐'),
                                    text:'包厢套餐',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shoproomset/index?room_id=" + row['id'];
                                    }
                                },

                                {
                                    name: 'list',
                                    title: __('预订记录'),
                                    text:'预订记录',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shoproombook/detail?room_id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('订单列表'),
                                    text:'订单列表',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "order/shoporder/index?room_id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('开门'),
                                    text:'开门',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shoproom/add?ids=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('关门'),
                                    text:'关门',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    confirm: '是否关灯',
                                    url: function (row) {
                                        return "shop/shoproom/close?ids=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('清除保洁'),
                                    text:'清除保洁',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: function (row) {
                                        return "shop/shoproom/clean?ids=" + row['id'];
                                    }
                                }

                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
